package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.server.service.AudioService;
import com.tanhua.server.vo.AudioVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("peachblossom")
public class AudioController {

    @Autowired
    private AudioService audioService;

    @PostMapping
    public ResponseEntity<Void> saveAudio(@RequestParam("soundFile") MultipartFile audioFile) {

        try {
            Boolean bool = this.audioService.saveAudio(audioFile);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //接收音频
    @GetMapping
    public ResponseEntity<AudioVo> queryAudio() {
        System.out.println("==============");
        try {
            AudioVo audioVo = this.audioService.queryAudio();
            if (ObjectUtil.isNotEmpty(audioVo)) {
                return ResponseEntity.ok(audioVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
